package com.zzusedating.androidapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zzusedating.androidapp.adapters.RecommendRecAdapter;
import com.zzusedating.androidapp.model.RecommendItem;
import com.zzusedating.androidapp.tools.BasicCallback;
import com.zzusedating.androidapp.tools.ConstantsTools;
import com.zzusedating.androidapp.tools.MyApplication;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class SpecSearchActivity extends BaseActivity {
    private static final int USER_NOT_FOUND = 5;
    private static final int SEARCH_SUCCESS = 1;

    private EditText searchView;

    private BasicCallback searchCallback;

    private RecyclerView recyclerView;
    private RecommendRecAdapter adapter;
    private ArrayList<RecommendItem> items;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case ConstantsTools.NETWORK_ERROR_I:
                    MyApplication.Toast(ConstantsTools.NETWORK_ERROR_);
                    findViewById(R.id.SpecSearch_tip).setVisibility(View.GONE);
                    break;
                case USER_NOT_FOUND:
                    findViewById(R.id.SpecSearch_tip).setVisibility(View.VISIBLE);
                    break;
                case SEARCH_SUCCESS:
                    findViewById(R.id.SpecSearch_tip).setVisibility(View.GONE);
                    adapter.notifyDataSetChanged();
                    break;
            }
            return true;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spec_search_layout);

        findViewById(R.id.SpecSearch_ConditionSearchView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable s = searchView.getText();
                if (TextUtils.isEmpty(s))
                    return;
                MyApplication.getClient().newCall(
                        new Request.Builder()
                                .get()
                                .url(String.format(ConstantsTools.SEARCH_NAME_EMAIL, s.toString()))
                                .build()
                ).enqueue(searchCallback);
            }
        });

        searchView = findViewById(R.id.SpecSearch_EditText);

        items = new ArrayList<>();
        adapter = new RecommendRecAdapter(items, this);
        recyclerView = findViewById(R.id.SpecSearch_RecyclerView);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        searchCallback = new BasicCallback(handler) {
            @Override
            public void onResponse(Call call, @NonNull Response response) {
                JSONArray jsonArray = (JSONArray) getObject(response);
                if (jsonArray != null){
                    List<RecommendItem> temp = (new Gson()).fromJson(jsonArray.toString(),
                            new TypeToken<List<RecommendItem>>(){}.getType());
                    items.clear();
                    items.addAll(temp);
                    handler.sendMessage(ConstantsTools.createMessage(SEARCH_SUCCESS));
                }
                else
                    handler.sendMessage(ConstantsTools.createMessage(getCode()));
            }
        };
    }
}
